local luomu = fk.CreateSkill {
  name = "th_luomu",
}

luomu:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(luomu.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local used = {}
    local ids = {}
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data
      if use.from == player and use.card then
        table.insertIfNeed(used, use.card.trueName)
      end
    end, Player.HistoryRound)
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse then
          for _, info in ipairs(move.moveInfo) do
            if room:getCardArea(info.cardId) == Card.DiscardPile and not table.contains(used, Fk:getCardById(info.cardId, true).trueName) then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    if #ids > 0 and room:askToSkillInvoke(player, {
      skill_name = luomu.name
    }) then
      local dat = room:askToUseRealCard(player, {
        pattern = ids,
        skill_name = luomu.name,
        prompt = "#th_luomu-use",
        expand_pile = ids,
        extra_data = {
          bypass_times = true,
          extraUse = false
        },
        cancelable = true,
        skip = true
      })
      if dat then
        event:setCostData(self, dat)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self))
  end
})

Fk:loadTranslationTable{
  ["th_luomu"] = "落木",
  [":th_luomu"] = "每回合结束时，你可以使用一张本回合不因使用而进入弃牌堆的牌（不能是你本轮使用过的牌名）。",

  ["#th_luomu-use"] = "落木：你可以使用一张本回合进入弃牌堆的牌（不能是你本轮使用过的牌名）",
}

return luomu